package com.ichina.chat.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SourceUrl {

	public Map<String, String> search(String sourceUrl)
			throws UnsupportedEncodingException {
		Map<String, String> searchvalue = new HashMap<String, String>();
		String zhongwen = "";
		if (sourceUrl.indexOf("http://www.baidu.com") >= 0) {
			int index = sourceUrl.indexOf("?wd");
			if (index > 0) {
				index = index + 4;
				String temp = sourceUrl.substring(index);
				if(temp.indexOf("&")>0){
					zhongwen = temp.substring(0, temp.indexOf("&"));
					searchvalue.put("keywords", java.net.URLDecoder.decode(
							zhongwen, "GBK"));
				}else{
					searchvalue.put("keywords", java.net.URLDecoder.decode(temp,
					"GBK"));
				}
			} else {
				index = sourceUrl.indexOf("&wd") + 4;
				String temp = sourceUrl.substring(index);
				//keywords = java.net.URLDecoder.decode(temp, "GBK");
				searchvalue.put("keywords", java.net.URLDecoder.decode(temp,
						"GBK"));
			}
			searchvalue.put("sourceUrl", "百度");
		} else if (sourceUrl.indexOf("http://www.google.com") >= 0) {
			int index = sourceUrl.indexOf("?q");
			if (index > 0) {
				index = index + 3;
				String temp = sourceUrl.substring(index);

				searchvalue.put("keywords", java.net.URLDecoder.decode(temp,
						"utf-8"));

			} else {
				index = sourceUrl.indexOf("&q") + 3;
				String temp = sourceUrl.substring(index);
				zhongwen = temp.substring(0, temp.indexOf("&"));
				//keywords = java.net.URLDecoder.decode(temp, "GBK");
				searchvalue.put("keywords", java.net.URLDecoder.decode(
						zhongwen, "utf-8"));
			}
			searchvalue.put("sourceUrl", "谷歌");
		} else if (sourceUrl.indexOf("http://www.soso.com/") >= 0) {
			int index;
			index = sourceUrl.indexOf("&w") + 3;
			String temp = sourceUrl.substring(index);
			if (temp.indexOf("&") > -1) {
				zhongwen = temp.substring(0, temp.indexOf("&"));
				searchvalue.put("keywords", java.net.URLDecoder.decode(
						zhongwen, "GBK"));
			} else {
				searchvalue.put("keywords", java.net.URLDecoder.decode(temp,
						"GBK"));
				//System.out.println(temp);
			}
			searchvalue.put("sourceUrl", "搜搜");
		} else if (sourceUrl.indexOf("http://www.qihoo.com") >= 0) {
			int index;
			index = sourceUrl.indexOf("?kw") + 4;
			String temp = sourceUrl.substring(index);
			zhongwen = temp.substring(0, temp.indexOf("&"));
			searchvalue.put("keywords", java.net.URLDecoder.decode(zhongwen, "GBK"));
			searchvalue.put("sourceUrl", "奇虎");
		} else {
			sourceUrl = "其他";
		}
		return searchvalue;
	}

	public Map<String, String> getClientInfo(String userAgentInfo) {
		String info = userAgentInfo.toUpperCase();
		Map<String, String> clientInfo = new HashMap<String, String>();
		String[] strInfo = info.substring(info.indexOf("(") + 1,
				info.indexOf(")") - 1).split(";");
		if ((info.indexOf("MSIE")) > -1) {
			clientInfo.put("userExplorer", strInfo[1].trim());
			clientInfo.put("userSystem", strInfo[2].trim());
		} else {
			String[] str = info.split(" ");
			if (info.indexOf("NAVIGATOR") < 0 && info.indexOf("FIREFOX") > -1) {
				clientInfo.put("userExplorer", str[str.length - 1].trim());
				clientInfo.put("userSystem", strInfo[2].trim());
			} else if ((info.indexOf("OPERA")) > -1) {
				clientInfo.put("userExplorer", str[0].trim());
				clientInfo.put("userSystem", strInfo[0].trim());
			} else if (info.indexOf("CHROME") < 0
					&& info.indexOf("SAFARI") > -1) {
				clientInfo.put("userExplorer", str[str.length - 1].trim());
				clientInfo.put("userSystem", strInfo[2].trim());
			} else if (info.indexOf("CHROME") > -1) {
				clientInfo.put("userExplorer", str[str.length - 2].trim());
				clientInfo.put("userSystem", strInfo[2].trim());
			} else if (info.indexOf("NAVIGATOR") > -1) {
				clientInfo.put("userExplorer", str[str.length - 1].trim());
				clientInfo.put("userSystem", strInfo[2].trim());
			} else {
				clientInfo.put("userExplorer", "Unknown Browser");
				clientInfo.put("userSystem", "Unknown OS");
			}
		}
		return clientInfo;
	}

}
